/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.generator.service;

import cn.fzh.hikari.multiDataSource.annotation.HikariDataSource;
import cn.fzh.hikari.multiDataSource.constants.HikariDataSourceType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.xrfzh.common.axios.AxiosEnum;
import cn.xrfzh.generator.mapper.MysqlGenMapper;
import cn.xrfzh.generator.utils.GenUtil2;
import cn.xrfzh.generator.utils.PageUtil;
import cn.xrfzh.generator.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 */
@Slf4j
@Service
@HikariDataSource(HikariDataSourceType.SLAVE_DATA_SOURCE)
public class MysqlGenService {

	@Resource
	private MysqlGenMapper mysqlGenMapper;

	@Resource
	private GenUtil2 genUtil2;

	public PageUtil queryList(Query query) {
		Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
		List<Map<String, Object>> list = mysqlGenMapper.queryList(query);
		int total = (int) page.getTotal();
		return new PageUtil(list, total, query.getLimit(), query.getPage());
	}

	public Map<String, String> queryTable(String tableName) {
		return mysqlGenMapper.queryTable(tableName);
	}

	public List<Map<String, String>> queryColumns(String tableName) {
		return mysqlGenMapper.queryColumns(tableName);
	}

	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		for (String tableName : tableNames) {
			// 查询表信息
			Map<String, String> table = queryTable(tableName);
			// 查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			// 生成代码
			// GenUtil.generatorCode(table, columns, zip);
			genUtil2.generatorCode(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public void download(String[] tableNames, HttpServletResponse response) {
		try {
			byte[] data = generatorCode(tableNames);
			response.reset();
			response.setHeader("Content-Disposition", "attachment; filename=\"generated_code.zip\"");
			response.addHeader("Content-Length", "" + data.length);
			response.setContentType("application/octet-stream; charset=UTF-8");
			IOUtils.write(data, response.getOutputStream());
		}
		catch (Exception e) {
			log.error(AxiosEnum.DOWNLOAD_ERROR.getMsg());
			e.printStackTrace();
		}
	}

}
